
// Make a namespace.
if (typeof Birdtune == 'undefined') {
  var Birdtune = {};
}

if (typeof Cc == 'undefined')
	var Cc = Components.classes;
if (typeof Ci == 'undefined')
	var Ci = Components.interfaces;
	
if (typeof SP == 'undefined')
	var SP = "http://songbirdnest.com/rdf/servicepane#";

const SC_NS_DT  = "http://songbirdnest.com/data/1.0#";	

function findRadioNode(node, radioString) {
	if (node.isContainer && node.name != null &&
		((radioString && node.name == radioString)
		 || (node.getAttributeNS(SC_NS_DT, "radioFolder") == 1)))
	{
		node.setAttributeNS(SC_NS_DT, "radioFolder", 1);
		return node;
	}

	if (node.isContainer) {
		var children = node.childNodes;
		while (children.hasMoreElements()) {
			var child =
					children.getNext().QueryInterface(Ci.sbIServicePaneNode);
			var result = findRadioNode(child, radioString);
			if (result != null)
				return result;
		}
	}
	return null;
}

/**
 * UI controller that is loaded into the main player window
 */
Birdtune.Controller = {

  /**
   * Called when the window finishes loading
   */
  onLoad: function() {

    // initialization code
    this._initialized = true;
    // this._strings = document.getElementById("birdtune-strings");
    
    // Perform extra actions the first time the extension is run
    if (Application.prefs.get("extensions.birdtune.firstrun").value) {
      Application.prefs.setValue("extensions.birdtune.firstrun", false);
      this._firstRunSetup();
    }
    
  const Cc = Components.classes;
  const Ci = Components.interfaces;
  if (typeof(FAVICON_PATH) == "undefined")
	const FAVICON_PATH = "chrome://birdtune/skin/pane-icon.png";
	
		// Create a service pane node for our chrome
		var SPS = Cc['@songbirdnest.com/servicepane/service;1'].
				getService(Ci.sbIServicePaneService);

    // Check whether the node already exists
    if (SPS.getNode("SB:RadioStations:BirdTune"))
      return;
      
		// Walk nodes to see if a "Radio" folder already exists
		var radioFolder = SPS.getNode("SB:RadioStations");
		if (!radioFolder) {
			radioFolder = SPS.createNode();
			radioFolder.id = "SB:RadioStations";
			radioFolder.className = "folder radio";
			radioFolder.name = SBString("servicesource.radio");
			radioFolder.setAttributeNS(this.SB_NS, "radioFolder", 1); // for backward-compat
			radioFolder.setAttributeNS(this.SP_NS, "Weight", 2);
			SPS.root.appendChild(radioFolder);
		}
		radioFolder.editable = false;
		radioFolder.hidden = false;
		
		// Add BirdTune chrome to service pane
    var node = SPS.createNode();
    node.url = "chrome://birdtune/content/accesspane.xul";
    node.id = "SB:RadioStations:BirdTune";
		node.name = "BirdTune";
		node.image = FAVICON_PATH;
		radioFolder.appendChild(node);
		node.editable = false;
    node.hidden = false;
		
		birdtuneUninstallObserver.register();
      
  },
  

  /**
   * Called when the window is about to close
   */
  onUnLoad: function() {
    this._initialized = false;
  },
  
  
  /**
   * Perform extra setup the first time the extension is run
   */
  _firstRunSetup : function() {
  
    // Call this.doHelloWorld() after a 3 second timeout
    setTimeout(function(controller) { controller.doHelloWorld(); }, 3000, this); 
  
  },
  
  

  
};

  var birdtuneUninstallObserver = {
	_uninstall : false,
	_disable : false,
	_tabs : null,

	observe : function(subject, topic, data) {
		var radioString =
			SBString("servicesource.radio");
		if (topic == "em-action-requested") {
			// Extension has been flagged to be uninstalled
			subject.QueryInterface(Ci.nsIUpdateItem);
			
			var SPS = Cc['@songbirdnest.com/servicepane/service;1'].
					getService(Ci.sbIServicePaneService);
			var radioNode = findRadioNode(SPS.root);
			var birdtuneNode = SPS.getNodeForURL(
						"chrome://birdtune/content/accesspane.xul");

			if (subject.id == "birdtune@geekshadow.com") {
				if (data == "item-uninstalled") {
					this._uninstall = true;
				} else if (data == "item-disabled") {
					this._disable = true;
					birdtuneNode.hidden = true;
				} else if (data == "item-cancel-action") {
					if (this._uninstall)
						this._uninstall = false;
					if (this._disable)
						birdtuneNode.hidden = false;
				}
			}
		} else if (topic == "quit-application-granted") {
			// We're shutting down, so check to see if we were flagged
			// for uninstall - if we were, then cleanup here
			if (this._uninstall) {

			}
			this.unregister();
		}
	},

	register : function() {
		var observerService = Cc["@mozilla.org/observer-service;1"]
			.getService(Ci.nsIObserverService);
		observerService.addObserver(this, "em-action-requested", false);
		observerService.addObserver(this, "quit-application-granted", false);
	},

	unregister : function() {
		var observerService = Cc["@mozilla.org/observer-service;1"]
			.getService(Ci.nsIObserverService);
		observerService.removeObserver(this, "em-action-requested");
		observerService.removeObserver(this, "quit-application-granted");
	}
}

window.addEventListener("load", function(e) { Birdtune.Controller.onLoad(e); }, false);
window.addEventListener("unload", function(e) { Birdtune.Controller.onUnLoad(e); }, false);
